package com.lx.algorithm.selectSort;

import com.lx.algorithm.util.SortUtils;

/**
 * Description: 选择排序
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-01 12:45:13
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-01     张李鑫                     1.0         1.0 Version
 */
public class SelectionSort {



    /**
     * <p>
     * 选择排序
     * 遍历数组 找到极值 放到一端
     * </p>
     *
     * @param arr
     * @return int[]
     * @Author zlx
     * @date 2021/9/1
     */
    public static int[] selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int j = i + 1;
            for (int i1 = i + 1; i1 < arr.length; i1++) {
                if (arr[j] > arr[i1]) {
                    j = i1;
                }
            }
            if (arr[j] < arr[i]) {
                SortUtils.swap(j,i,arr);
            }
        }
        return arr;
    }

    public static int [] selectionSortByTeach(int []arr){
        for (int i = 0; i < arr.length-1; i++) {
            int min=i;
            for (int i1 = i+1; i1 < arr.length; i1++) {
               min= arr[min]>arr[i1]?i1:min;
            }
            if (min!=i) {
                SortUtils.swap(i, min, arr);
            }
        }
        return arr;
    }
}
